<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i>试题管理</el-breadcrumb-item>
        <el-breadcrumb-item>试题管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">

        <el-row>
          <el-col :span="4">
            <div class="grid-content bg-purple">
              <el-button type="primary"
                         icon="el-icon-delete"
                         class="handle-del mr10"
                         @click="delAllSelection">批量删除</el-button>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple">
              试卷名称：<el-input v-model="query.name"
                        placeholder="试卷名称"
                        class="handle-input mr10"></el-input>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple">
              <!-- 试卷分类：<el-input v-model="query.categoryid"
                        placeholder="试卷分类"
                        class="handle-input mr10"></el-input> -->
                        试卷分类：
                         <el-select v-model="query.categoryid"
                             placeholder="课程分类"
                             style="">
                    <el-option v-for="item in categoryList"
                               :key="item.id"
                               :label="item.categoryname"
                               :value="item.id">
                    </el-option>
                  </el-select>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple">
              创建人 ：<el-input v-model="query.creatername"
                        placeholder="创建人"
                        class="handle-input mr10"></el-input>
            </div>
          </el-col>
          <el-col :span="2">
            <div class="grid-content bg-purple">
              <el-button type="primary"
                         icon="el-icon-search"
                         @click="handleSearch">搜索</el-button>
            </div>
          </el-col>
        </el-row>
      </div>
      <el-table :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange">
        <el-table-column type="selection"
                         width="55"
                         align="center"></el-table-column>
        <el-table-column prop="id"
                         label="ID"
                         width="55"
                         align="center"></el-table-column>
        <el-table-column prop="name"
                         label="试卷名称"></el-table-column>
        <el-table-column prop="categoryid"
                         label="试卷分类"
                         align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.categoryid==1">java</span>
            <span v-if="scope.row.categoryid==2">vue</span>
            <span v-if="scope.row.categoryid==3">C++</span>
            <span v-if="scope.row.categoryid==4">C</span>
            <span v-if="scope.row.categoryid==5">Python</span>
            <span v-if="scope.row.categoryid==6">VB</span>
          </template>
        </el-table-column>
        <el-table-column prop="creatername"
                         label="创建人"></el-table-column>
        <el-table-column prop="time"
                         label="考试时长">
          <template scope="scope">
            {{scope.row.time}}分钟
          </template>
        </el-table-column>
        <el-table-column label="操作"
                         width="180"
                         align="center">
          <template slot-scope="scope">
            <el-button type="text"
                       icon="el-icon-edit"
                       @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="text"
                       icon="el-icon-delete"
                       class="red"
                       @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background
                       layout="total, prev, pager, next"
                       :current-page="pageparm.currentPage"
                       :page-size="pageparm.pageSize"
                       :total="pageparm.total"
                       @current-change="handlePageChange"></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑"
               :visible.sync="editVisible"
               width="30%">
      <el-form ref="form"
               :model="form"
               label-width="70px">
        <el-form-item label="试卷名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="试卷分类">
          <el-select v-model="form.categoryid"
                             placeholder="课程分类"
                             style="">
                    <el-option v-for="item in categoryList"
                               :key="item.id"
                               :label="item.categoryname"
                               :value="item.id">
                    </el-option>
                  </el-select>
        </el-form-item>
        <el-form-item label="创建人">
          <el-input v-model="form.creatername"></el-input>
        </el-form-item>
        <el-form-item label="考试时长">
          <el-input v-model="form.time"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
    name: 'TopicManage',
    data() {
        return {
          categoryList: [],
            query: {
                name: '',
                categoryid: '',
                creatername: '',
                status:'y',
            },
            // editForm: {
            //     trainingname:"",
            //     establishtime:"",
            //     status:'',
            //     establish:""
            // },
            // 分页参数
            pageparm: {
                currentPage: 1,
                pageSize: 5,
                total: 5
            },
            tableData: [],
            current: '',
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData(this.pageparm);
        this.findCategory();
    },
    methods: {
       findCategory() {
            this.$axios
                .post('/api/category/findCategory/', {})
                .then((res) => {
                  if(res.data.code==200){
                    this.categoryList = res.data.data;
                  }
                 })
                .catch();
        },
        // 获取 培训项目数据
        getData(parameter) {
            // fetchData(this.query).then(res => {
            //     console.log(res);
            //     this.tableData = res.list;
            //     this.pageTotal = res.pageTotal || 50;
            // });

            if (this.query.categoryid == 'java') {
                this.query.categoryid = 1;
            } else if (this.query.categoryid == 'vue') {
                this.query.categoryid = 2;
            } else if (this.query.categoryid == 'C++') {
                this.query.categoryid = 3;
            } else if (this.query.categoryid == 'C') {
                this.query.categoryid = 4;
            } else if (this.query.categoryid == 'Pyson') {
                this.query.categoryid = 5;
            } else if (this.query.categoryid == 'VB') {
                this.query.categoryid = 6;
            }
            console.log(this.query.categoryid);
            this.$axios
                .post('/api/topic/findTopic/' + this.pageparm.currentPage + '/' + this.pageparm.pageSize, this.query)
                .then((res) => {
                    this.tableData = res.data.data.list;
                    // for (let i = 0; i < this.tableData.length; i++) {
                    //     if (this.tableData[i].categoryid == 1) {
                    //         this.tableData[i].categoryid = 'java';
                    //     } else if (this.tableData[i].categoryid == 2) {
                    //         this.tableData[i].categoryid = 'vue';
                    //     } else if (this.tableData[i].categoryid == 3) {
                    //         this.tableData[i].categoryid = 'C++';
                    //     } else if (this.tableData[i].categoryid == 4) {
                    //         this.tableData[i].categoryid = 'C';
                    //     } else if (this.tableData[i].categoryid == 5) {
                    //         this.tableData[i].categoryid = 'Phson';
                    //     } else if (this.tableData[i].categoryid == 6) {
                    //         this.tableData[i].categoryid = 'VB';
                    //     }
                    // }
                    this.pageparm.total = res.data.data.total;
                })
                .catch((err) => {
                    console.log(err);
                });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.pageparm, 'currentPage', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$axios
                        .delete('/api/topic/deleteTopic/' + row.id)
                        .then((res) => {
                            //console.log(res);
                            if (res.data.code == 200) {
                                this.$message({
                                    type: 'success',
                                    message: '数据已删除!'
                                });
                                //重新获取当前页面数据重新渲染列表！
                                this.getData(this.pageparm);
                            } else {
                                this.$message({
                                    type: 'info',
                                    message: res.data.msg
                                });
                            }
                        })
                        .catch((err) => {
                            //this.$message.error('数据删除失败，请稍后再试！')
                            console.log(err);
                        });
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        // 多选操作
        handleSelectionChange(val) {
            //console.log(val);
            this.multipleSelection = val;
        },
        delAllSelection() {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    //console.log(this.multipleSelection);
                    const length = this.multipleSelection.length;
                    let str = '';
                    this.delList = this.delList.concat(this.multipleSelection);
                    for (let i = 0; i < length; i++) {
                        str += this.multipleSelection[i].name + ' ';
                        // console.log(this.multipleSelection[i].id);
                        this.$axios
                            .delete('/api/topic/deleteTopic/' + this.multipleSelection[i].id)
                            .then((res) => {
                                //console.log(res);
                                if (res.data.code == 200) {
                                    this.$message({
                                        type: 'success',
                                        message: '数据已删除!'
                                    });
                                    //重新获取当前页面数据重新渲染列表！
                                    this.getData(this.pageparm);
                                } else {
                                    this.$message({
                                        type: 'info',
                                        message: res.data.msg
                                    });
                                }
                            })
                            .catch((err) => {
                                //this.$message.error('数据删除失败，请稍后再试！')
                                console.log(err);
                            });
                        //console.log(str);
                    }
                    this.$message.error(`删除了${str}`);
                    this.multipleSelection = [];
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        // 编辑操作
        handleEdit(index, row) {
            //console.log(row);
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            //this.$set(this.tableData, this.idx, this.form);
            this.$axios
                .put('/api/topic/updateTopic/', this.form)
                .then((res) => {
                    if (res.data.code == 200) {
                        this.getData(this.pageparm);
                        this.$message({
                            type: 'success',
                            message: '数据修改成功！'
                        });
                        this.editFormVisible = false;
                    } else {
                        this.$message({
                            type: 'info',
                            message: res.data.msg
                        });
                        this.editFormVisible = false;
                    }
                })
                .catch();
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.pageparm, 'currentPage', val);
            //console.log(val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.el-input {
    position: relative;
    font-size: 14px;
    display: inline-block;
    width: 180px !important;
}
.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
